using MediatR;
using Microsoft.AspNetCore.Mvc;
using WMS.Domain;
using WMS.ErrorCode;
using WMS.Infrasturcture.Interfaces;
using WMS.WebApiRead.Application.Command;
using WMS.WebApiRead.DTO;

namespace WMS.WebApiRead.Controllers
{
    /// <summary>
    /// 盘点类型读操作控制器（查询）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventoryTypeController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<InventoryTypeController> logger;
        private readonly IBaseRepository<InventoryTypeInfo> inventoryTypeRepository;

        /// <summary>
        /// 构造函数，注入MediatR、日志服务和仓储
        /// </summary>
        public InventoryTypeController(
            IMediator mediator,
            ILogger<InventoryTypeController> logger,
            IBaseRepository<InventoryTypeInfo> inventoryTypeRepository)
        {
            this.mediator = mediator;
            this.logger = logger;
            this.inventoryTypeRepository = inventoryTypeRepository;
        }

        /// <summary>
        /// 查询所有盘点类型列表（用于下拉选择框等场景）
        /// </summary>
        /// <returns>返回盘点类型列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<InventoryTypeInfoResultDTO>>> GetInventoryTypeList()
        {
            logger.LogInformation("开始查询盘点类型列表");

            return await mediator.Send(new InventoryTypeQueryCommand());
        }

        /// <summary>
        /// 根据盘点类型ID查询盘点类型详情
        /// </summary>
        /// <param name="inventoryTypeId">盘点类型ID</param>
        /// <returns>返回盘点类型详情</returns>
        [HttpGet]
        public async Task<ApiResult<InventoryTypeInfoResultDTO>> GetInventoryTypeById([FromQuery] int inventoryTypeId)
        {
            logger.LogInformation($"开始查询盘点类型详情，盘点类型ID：{inventoryTypeId}");

            try
            {
                var inventoryType = inventoryTypeRepository.GetAll()
                    .Where(x => x.InventoryTypeId == inventoryTypeId)
                    .Select(x => new InventoryTypeInfoResultDTO
                    {
                        InventoryTypeId = x.InventoryTypeId,
                        InventoryTypeName = x.InventoryTypeName
                    })
                    .FirstOrDefault();

                if (inventoryType == null)
                {
                    return new ApiResult<InventoryTypeInfoResultDTO>
                    {
                        Code = ApiResultEnum.查询失败,
                        Message = "盘点类型不存在",
                        Data = null
                    };
                }

                return new ApiResult<InventoryTypeInfoResultDTO>
                {
                    Code = ApiResultEnum.查询成功,
                    Message = "查询盘点类型详情成功",
                    Data = inventoryType
                };
            }
            catch (Exception ex)
            {
                logger.LogError($"查询盘点类型详情异常：{ex.Message}");
                return new ApiResult<InventoryTypeInfoResultDTO>
                {
                    Code = ApiResultEnum.查询失败,
                    Message = $"查询盘点类型详情异常：{ex.Message}",
                    Data = null
                };
            }
        }
    }
}

